/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.core.wicket.resource;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

import org.apache.wicket.markup.html.image.resource.DynamicImageResource;

/**
 * <pre>
 * 
 * 验证码图片资源
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class CaptchaImageResource extends DynamicImageResource {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 2995315448043495198L;

	/**
	 * 图片的高
	 */
	private int height = 20;

	/**
	 * 图片的宽
	 */
	private int width = 80;

	/**
	 * 验证码对象
	 */
	private Captcha captcha;

	/**
	 * 构造函数，验证码有效时间为默认设置
	 */
	public CaptchaImageResource() {
		this.captcha = new Captcha();
	}

	/**
	 * 构造函数，验证码有效时间为默认设置
	 * 
	 * @param width
	 *            验证码图片宽度
	 * @param height
	 *            验证码图片高度
	 */
	public CaptchaImageResource(int width, int height) {
		this.width = width;
		this.height = height;
		this.captcha = new Captcha();
	}

	/**
	 * 构造函数，以验证码对象作为参数，外部可先构造验证码对象设置有效期
	 * 
	 * @param captcha
	 *            验证码对象
	 */
	public CaptchaImageResource(Captcha captcha) {
		this.captcha = captcha;
	}

	/**
	 * 构造函数，以验证码对象作为参数，外部可先构造验证码对象设置有效期
	 * 
	 * @param width
	 *            验证码图片宽度
	 * @param height
	 *            验证码图片高度
	 * @param captcha
	 *            验证码对象
	 */
	public CaptchaImageResource(int width, int height, Captcha captcha) {
		this.width = width;
		this.height = height;
		this.captcha = captcha;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see org.apache.wicket.markup.html.image.resource.DynamicImageResource#
	 * getImageData()
	 */
	@Override
	protected byte[] getImageData() {
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

		// 创建Graphics实例
		Graphics graphics = image.createGraphics();

		// 绘制背景
		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, width, height);

		// 绘制边框
		graphics.setColor(Color.BLACK);
		graphics.drawRect(0, 0, width - 1, height - 1);

		// 绘制干扰线
		graphics.setColor(Color.GRAY);
		Random random = new Random();
		for (int i = 0; i < 20; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int xl = random.nextInt(12);
			int yl = random.nextInt(12);
			graphics.drawLine(x, y, x + xl, y + yl);
		}

		// 设置字体
		graphics.setFont(new Font("Courier New", Font.BOLD, 18));
		FontMetrics fontMetrics = graphics.getFontMetrics();

		// 绘制验证码
		StringBuffer randomCode = new StringBuffer();
		int red = 0, green = 0, blue = 0;
		int fontCount = 5;
		for (int i = 0; i < fontCount; i++) {

			String randomString;
			if (random.nextInt(10) > 4)
				// 生成数字
				randomString = String.valueOf(random.nextInt(10));
			else {
				// 生成字符
				char ch = (char) randomInt(65, 90); // 97 122
				randomString = String.valueOf(ch);
			}

			// 生成随即颜色,不能写成２５５，防止生成白色
			red = random.nextInt(200);
			green = random.nextInt(200);
			blue = random.nextInt(200);
			graphics.setColor(new Color(red, green, blue));

			// 计算字体的x坐标,y坐标
			int x = (width / (fontCount + 1)) * i + fontMetrics.charWidth(randomString.toCharArray()[0]);
			int y = randomInt(fontMetrics.getAscent(), height);

			// 绘制随即字
			graphics.drawString(randomString, x, y);
			randomCode.append(randomString);
		}
		graphics.dispose();

		captcha.setExpected(randomCode.toString());

		// Generate image
		return toImageData(image);
	}

	/**
	 * 随机生成指定区间的数
	 * 
	 * @param min
	 *            区间的开头
	 * @param max
	 *            　区间的结尾
	 * @return
	 */
	protected int randomInt(int min, int max) {
		return (int) (Math.random() * (double) (max - min) + (double) min);
	}

	public Captcha getCaptcha() {
		return captcha;
	}
}
